package cn.com.generaldata.os.service.indicator_evaluation.industry_spread_effect;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.Validate;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.impl.sql.SqlTemplate;
import org.nutz.dao.sql.Criteria;
import org.nutz.lang.random.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.generaldata.jsme_indicator_system.entity.Dustry;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorConsumptResult;
import cn.com.generaldata.jsme_indicator_system.entity.IndicatorEvaluation;
import cn.com.generaldata.os.service.impl.DustryService;
import cn.com.generaldata.os.service.impl.IndicatorConsumptService;
import cn.com.generaldata.os.service.indicator_constant.ProvinceIndicatorConstant;
import cn.com.generaldata.os.service.indicator_evaluation.CalculateService;
import cn.com.generaldata.os.service.indicator_evaluation.CalculateServiceHelper;
import cn.com.generaldata.os.util.SpringContextHolder;
import cn.com.generaldata.os.vo.IndicatorAccountVO;

import com.google.common.collect.ImmutableMap;

/**
 * 后向关联系数运算类.
 */
public class BackConnectionCoefficientCalculateService implements CalculateService {

	private final String backConnection = ProvinceIndicatorConstant.BACKCONNECT;

	private final Logger logger = LoggerFactory.getLogger(getClass());

	private Dao dao;

	private Dao indicatorSystemDao;

	private CalculateServiceHelper helper;

	// 计算指标方法
	@Override
	public void calculateIndicator(IndicatorEvaluation indicator, IndicatorAccountVO indicatorAccountVO) {
		DustryService dustryService = SpringContextHolder.getBean(DustryService.class);
		IndicatorConsumptService indicatorConsumptService = SpringContextHolder.getBean(IndicatorConsumptService.class);

		Validate.isTrue(indicatorConsumptService.checkHasData(indicatorAccountVO.getYear()), "不存在该年份的直接消耗系数表数据");

		SqlTemplate sqlTemplate = new SqlTemplate(dao);

		List<Dustry> dustrys = dustryService.getEnableDustrys(indicatorAccountVO.getYear());

		for (Dustry dustry : dustrys) {
			String sql2 = "select sum(consumpt_value) from tbl_indicator_consumpt where foreige_id=@foreigeId and year=@year";
			Map<String, Object> params = ImmutableMap.<String, Object> of("foreigeId", dustry.getId(), "year",
					indicatorAccountVO.getYear());
			Double sumValue = sqlTemplate.queryForObject(sql2, params, Double.class);

			IndicatorConsumptResult ier = new IndicatorConsumptResult();
			ier.setId(R.UU64());
			ier.setDustryId(dustry.getId());
			ier.setSubTypeName(backConnection);
			ier.setTypeName("直接关联指数");
			ier.setIndicatorValue(sumValue);
			ier.setYear(indicatorAccountVO.getYear());
			Criteria cri = getIndicatorDataCri(ier);
			IndicatorConsumptResult record = dao.fetch(IndicatorConsumptResult.class, cri);
			if (record != null) {
				dao.clear(IndicatorConsumptResult.class, cri);
			}
			dao.insert(ier);
		}

	}

	private Criteria getIndicatorDataCri(IndicatorConsumptResult icr) {
		Criteria cri = Cnd.cri();
		cri.where().and("dustryId", "=", icr.getDustryId());
		cri.where().and("subTypeName", "=", icr.getSubTypeName());
		cri.where().and("year", "=", icr.getYear());
		return cri;
	}

	@Override
	public void setDao(Dao dao) {
		this.dao = dao;
	}

	@Override
	public void setIndicatorSystemDao(Dao indicatorSystemDao) {
		this.indicatorSystemDao = indicatorSystemDao;
	}

	@Override
	public void setCalculateServiceHelper(CalculateServiceHelper calculateServiceHelper) {
		// TODO Auto-generated method stub

	}

}
